import { Controller, UseGuards, Post, Body, Req, Put, Get, Param } from '@nestjs/common';
import { User } from '@libs/db/model/user.model';
import { InjectModel } from 'nestjs-typegoose';
import { ModelType } from '@typegoose/typegoose/lib/types';
import { Crud } from 'nestjs-mongoose-crud'
import { ApiTags, ApiBearerAuth, ApiOperation, ApiParam, ApiProperty } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { JwtService } from '@nestjs/jwt';

class loginDto{
    @ApiProperty()
    password:string
}
@Crud({
    model:User,
    routes:{
        findOne:false,
        find:false,
        delete:false,
        update:false
    }
})
@Controller('auth')
@ApiTags("登录")
export class AuthController {
    constructor(
        private readonly jwtService:JwtService,
        @InjectModel(User) public model: ModelType<User>) {}
    
    @ApiOperation({})
    @Get("UsernameIsExit/:username")
    async UsernameIsExit(@Param("username") username:string){
        
            const data=await this.model.findOne({username:username})
            if(data){
                return{
                    'code':100,
                    'msg':'已经存在该用户'
                }
            }else
            return{
                'code':200,
                'msg':'正常使用'
            }
        
    }
    @UseGuards(AuthGuard('local'))
    @Post("/login")
    async Login(@Body() body:{},@Req() req){

        return{
            'statusCode':200,
            'msg':"成功登录",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
            'token':this.jwtService.sign(String(req.user._id))
        }
    }


    @UseGuards(AuthGuard('jwt'))
    @Post("/getInfo")
    async getInfo(@Req() req){
        const user=await this.model.findById(req.user.id).populate('role').populate('classes')
        return{
            'statusCode':200,
            'data':user
        }
    }
    
   
    @Put("/updatePass/:email")
    async updatePass(@Param("email") email:string,@Body() Body:loginDto){
        const user=await this.model.findOneAndUpdate({'email':email},{
            password:Body.password
        })
        return{
            'statusCode':200,
            'msg':'修改成功'
        }
    }
}
